package com.hopu.oa.controller.work;

import com.hopu.oa.entity.work.workDept;
import com.hopu.oa.entity.work.worker;
import com.hopu.oa.service.work.workerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Controller
public class workController {

    @Autowired
    private workerServiceImpl workerService;

    @RequestMapping("worker")
    public String index(Model model, String deptName, String find,
                        @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "size", defaultValue = "4") int size
    ) {
        System.out.println("find = " + find);
        List<workDept> workDepts = workerService.selectDept();
        //总数量
        int totalElements = workerService.selectAllWoker(0, 0, deptName, find).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<worker> workers = workerService.selectAllWoker((page - 1) * size, size, deptName, find);

        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        System.out.println(workers);
        model.addAttribute("workDepts", workDepts);
        model.addAttribute("page", map1);
        model.addAttribute("find", find);
        model.addAttribute("workers", workers);
        model.addAttribute("url", "workbody");
        return "work/worker";
    }

}
